home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / misc / emu / SaveNames1_2.lha / SaveNames1.2 / SaveNames.c < prev    next >
C/C++ Source or Header  |  1995-06-10  |  14KB  |  497 lines

  1.  
  2. /**************************************************************************
  3.  *                                                                        *
  4.  *                            Save Names 1.2                              *
  5.  *                           ----------------                             *
  6.  *                                                                        *
  7.  *  SaveNames is freely distributable but is copyrighted by its author,   *
  8.  * Marcin Orlowski (carlos@felix.univ.szczecin.pl). For more information  *
  9.  * read the short documentation included in this archive. SaveNames is    *
  10.  * written in ANSI C, so may be compiled on any machine, except for pc    *
  11.  * (do you see any sense? :). Unfortunately small changes must be done    *
  12.  * befor you run compiler, so look below, and select your platform.       *
  13.  * AMIGA side was tested on A4000, UNIX side was tested on IRIS INDIGO,   *
  14.  * HP. The LINUX side was tested... on LINUX :-).                         *
  15.  *                                                                        *
  16.  * Use and enjoy.                                                         *
  17.  *                                                                        *
  18.  * Any suggestions, bugs, mails or anything else send to:                 *
  19.  *                                                                        *
  20.  * Marcin Orlowski                                                        *
  21.  * Radomska 38                                                            *
  22.  * 71-002 Szczecin                                                        *
  23.  * Poland                                                                 *
  24.  *                                                                        *
  25.  * or via net:                                                            *
  26.  *                                                                        *
  27.  * Internet: carlos@felix.univ.szczecin.pl                                *
  28.  * FidoNet:       Marcin Orlowski@2:481/22.2                              *
  29.  * GlobalNet:     Marcin Orlowski@52:4800/6                               *
  30.  *                                                                        *
  31.  * or try Silver Dream!'s BBS (non-stop) at +48 91 540431                 *
  32.  *                                                                        *
  33.  *************************************************************************/
  34.  
  35. /* Define your favourite platform (except Amiga :-) */
  36.  
  37. #define AMIGA
  38.  
  39. /*                     NOTE: LINUX automagically defines UNIX by itself,
  40.                                so you don't need to uncomment it by hand
  41. #define AMIGA
  42. #define UNIX
  43. #define LINUX
  44. */
  45.  
  46. /* Uncomment this only for compilers requiring C&R declaration type */
  47. /*
  48. #define OLD_STYLE
  49. */
  50.  
  51. /*************************************************************************/
  52.  
  53. #ifdef AMIGA
  54. char __stdiowin[] = "con:0/15/500/230/W.F.M.H. SaveNames";
  55. #define CURRENTDIR NULL
  56. #include <sys/dir.h>
  57.  
  58. #define EXIT_WARN 5
  59.  
  60. /* Do not touch this if you don't know what you are doing! */
  61. /*
  62. #ifdef MAXNAMELEN
  63. #undef MAXNAMELEN
  64. #endif
  65. #define MAXNAMELEN 108
  66. */
  67. #endif
  68.  
  69. /*************************************************************************/
  70.  
  71. #ifdef LINUX
  72. #include <unistd.h>
  73. #ifndef UNIX
  74. #define UNIX
  75. #endif
  76. #endif
  77.  
  78. /*************************************************************************/
  79.  
  80. #ifdef UNIX
  81. #ifdef CURRENTDIR
  82. #undef CURRENTDIR
  83. #endif
  84. #define CURRENTDIR "."
  85. #define PARENTDIR ".."
  86. #include <dirent.h>
  87. #include <sys/types.h>
  88. #endif
  89.  
  90. /*************************************************************************/
  91.  
  92. #include <sys/stat.h>
  93. #include <fcntl.h>
  94.  
  95. #include <stdio.h>
  96. #include <stdlib.h>
  97. #include <string.h>
  98. #include <ctype.h>
  99.  
  100. #ifndef TRUE
  101. #define TRUE 1
  102. #endif
  103. #ifndef FALSE
  104. #define FALSE ~TRUE
  105. #endif
  106.  
  107. /*************************************************************************/
  108.  
  109. #define    AMI_DD_OFS  853000                /* Capacity of various floppy disks   */
  110. #define    AMI_DD_FFS  880000        /* This values may not fit exactly,   */
  111. #define    AMI_HD_FFS 1780000        /* but it doesn't matter so much...   */
  112. #define    PC_DD              725000                /* In case of big number of files,    */
  113. #define    PC_HD             1455000          /* the disk space is going down, 'cos */
  114.                                   /* of file headers and similar things */
  115.  
  116. #define    PCNAME    12                                 /* 8 + . + 3 */
  117.  
  118. FILE    *FileHandle = NULL;
  119. DIR    *dfd;                                            /* directory descriptor */
  120. int    fh;                                                /* FileHandle */
  121. struct    dirent    *dptr;                        /* dir entry */
  122. struct    stat        *st = NULL;                /* file information */
  123.  
  124. char    TempBuff[PCNAME+1];                /* +1 because of trailing NULL */
  125. char    CurrentDir[MAXNAMLEN+2];
  126.  
  127. struct    Entry    *FirstEntry= NULL;    /* for queue of entrires... */
  128. struct  Entry    *LastEntry = NULL;
  129. struct    Entry *TempEntry = NULL;
  130. struct  Entry *Temp2         = NULL;
  131.  
  132. struct    Entry                                            /* ... like this one...  */
  133. {
  134.     struct    Entry *Next;                        /* Successor             */
  135.     char        INode[PCNAME+1];                /* Almost unique file ID */
  136.     char        Name[MAXNAMLEN+1];            /* Oryginal file name    */
  137. };
  138.  
  139. int    Index;                                        /* Universal counter    */
  140. int    Count = 0, SkipCount = 0;    /* Almost universal counters :) */
  141. unsigned long TotalSize = 0;            /* Guess yourself... */
  142. char    ErrorHeader = TRUE;
  143. char    DecodeResult;
  144.  
  145. /*************************************************************************/
  146.  
  147. #ifdef OLD_STYLE
  148. void main(args, argv)
  149. int args;
  150. char **argv;
  151. #else
  152. void main(int args, char **argv)
  153. #endif
  154. {
  155.     printf("SaveNames 1.2 by Marcin Orlowski\n");
  156.     printf("E-Mail: carlos@felix.univ.szczecin.pl\n\n");
  157.  
  158.     if(args != 3)
  159.         {
  160. #ifdef AMIGA
  161.         if(args == 0)
  162.             printf("** Can't be run from Workbench.\n\n");
  163. #endif
  164.  
  165. #ifdef    UNIX
  166.         printf("Type 'man SaveNames'");
  167. #else
  168.         printf("Read file 'SaveNames.doc'");
  169. #endif
  170.  
  171.         printf(" to get whole documentation\n\n");
  172.  
  173.         if(strlen(argv[0]))
  174.             printf("Usage: %s ", argv[0]);
  175.         else
  176.             printf("Shell Usage: SaveNames ");
  177.  
  178.         printf("Command DirName\n");
  179.         printf("Command - type 'C'  or 'CODE' to crypt names\n");
  180.         printf("          type '-C' or '-COUNT' to count disks usage only\n");
  181.         printf("          type 'D'  or 'DECODE' to restore names\n");
  182.         printf("          type '-D' or '-DECODE' for expanded restore\n");
  183.         printf("DirName - process contents of this directory\n");
  184.  
  185.         exit(EXIT_FAILURE);
  186.         }
  187.  
  188.  
  189.     getcwd(CurrentDir, sizeof(CurrentDir));
  190.     if(chdir(argv[2]))
  191.         {
  192.         printf("** Can't find directory.\n");
  193.         exit(EXIT_FAILURE);
  194.         }
  195.  
  196.  
  197.  
  198.     if(argv[1][0] == 'C' || argv[1][0] == 'c' || (argv[1][0] == '-' && argv[1][1] == 'C') || (argv[1][0] == '-' && argv[1][1] == 'c'))
  199.         {
  200.         if((dfd = opendir(CURRENTDIR)))
  201.             {
  202.             char    running = TRUE;
  203.  
  204.                 FileHandle = NULL;
  205.                 if((FileHandle = fopen("WFMHFile.idx", "r")) == NULL)
  206.                     if((FileHandle = fopen("wfmhfile.idx", "r")) == NULL)
  207.                         FileHandle = fopen("WFMHFILE.IDX", "r");
  208.  
  209.             if(argv[1][0] == 'C' && argv[1][0] == 'c')
  210.                 {
  211.                 if(FileHandle)
  212.                     {
  213.                     fclose(FileHandle);
  214.                     printf("** Index file found in drawer '%s'.\n", argv[2]);
  215.                     printf("** It means you have already coded filenames.\n");
  216.                     printf("** Because this operation cannot be nested\n");
  217.                     printf("** it has been aborted. Watch your moves!\n");
  218.                     chdir(CurrentDir);
  219.                     exit(EXIT_FAILURE);
  220.                     }
  221.                 }
  222.  
  223.  
  224.             if(argv[1][0] == '-')
  225.                 printf("Counting floppy disks usage ...\n");
  226.             else
  227.                 printf("Coding filenames...\n");
  228.  
  229.             Count = 0;
  230.             if((st = malloc(sizeof(struct stat))) == NULL)
  231.                 printf(" ** No length counting. No memory for structure.\n");
  232.  
  233.                 while((dptr = readdir(dfd)) && running)
  234.                     {
  235.  
  236.  
  237.  
  238.  
  239.                     if((TempEntry = (struct Entry *)malloc(sizeof(struct Entry))))
  240.                         {
  241.                         strcpy(TempEntry->Name, dptr->d_name);
  242.                         sprintf(TempEntry->INode, "%lx.pcp", dptr->d_ino);
  243.  
  244.                         if(!(FirstEntry))
  245.                             FirstEntry = TempEntry;
  246.                         else
  247.                             LastEntry->Next = TempEntry;
  248.                         LastEntry = TempEntry;
  249.                         TempEntry->Next = NULL;
  250.  
  251.                         if(st)
  252.                             if(fh = open(dptr->d_name, O_RDONLY, 0))
  253.                                 {
  254.                                 if(fstat(fh, st) == 0)
  255.                                     TotalSize += st->st_size;
  256.                                 close(fh);
  257.                                 }
  258.  
  259.                         Count++;
  260.                         }
  261.                     else
  262.                         {
  263.                         printf("** Out of memory. %d files scanned.\n", Count);
  264.                         printf("** Skipping the rest of files.\n");
  265.                         running = FALSE;
  266.                         }
  267.                     }
  268.  
  269.             closedir(dfd);
  270.  
  271.             if(st)
  272.                 {
  273.                 if(TotalSize != 0)
  274.                     {
  275.                     printf("\n Disks usage summary for %ld files (%ld bytes, %ld KB, %ld MB)\n\n", Count, TotalSize, (TotalSize/1024), (TotalSize/1048576));
  276.                     printf(" Disk type       Cnt  Free on last\n");
  277.                     printf(" ---------------------------------\n");
  278.                     printf(" Amiga DD (OFS) %3ld   %9ld\n", (AMI_DD_OFS + TotalSize) / AMI_DD_OFS, AMI_DD_OFS * ((AMI_DD_OFS + TotalSize) / AMI_DD_OFS) - TotalSize);
  279.                     printf(" Amiga DD (FFS) %3ld   %9ld\n", (AMI_DD_FFS + TotalSize) / AMI_DD_FFS, AMI_DD_FFS * ((AMI_DD_FFS + TotalSize) / AMI_DD_FFS) - TotalSize);
  280.                     printf(" Amiga HD (FFS) %3ld   %9ld\n", (AMI_HD_FFS + TotalSize) / AMI_HD_FFS, AMI_HD_FFS * ((AMI_HD_FFS + TotalSize) / AMI_HD_FFS) - TotalSize);
  281.                     printf(" pc    DD       %3ld   %9ld\n", (PC_DD + TotalSize) / PC_DD, PC_DD * ((PC_DD + TotalSize) / PC_DD) - TotalSize);
  282.                     printf(" pc    HD       %3ld   %9ld\n\n", (PC_HD + TotalSize) / PC_HD, PC_HD * ((PC_HD + TotalSize) / PC_HD) - TotalSize);
  283.                     }
  284.                 else
  285.                     printf("No files - no summary :-(...\n");
  286.                 }
  287.     
  288.         if(st)
  289.                 {
  290.                 free(st);
  291.                 st = NULL;
  292.                 }
  293.  
  294.             if(argv[1][0] == '-')                                    /* We wanted summary only */
  295.                 {
  296.                 TempEntry = FirstEntry;
  297.                 while(TempEntry)
  298.                     {
  299.                     Temp2 = TempEntry->Next;
  300.                     free(TempEntry);
  301.                     TempEntry = Temp2;
  302.                     }
  303.                 chdir(CurrentDir);
  304.                 exit(EXIT_SUCCESS);
  305.                 }
  306.  
  307.             if(FileHandle = fopen("WFMHFile.idx", "w"))
  308.                 {
  309.                 Count = 0;
  310.                 TempEntry = FirstEntry;
  311.  
  312.                 while(TempEntry)
  313.                     {
  314.                     if(rename(TempEntry->Name, TempEntry->INode))
  315.                         {
  316. #ifdef UNIX
  317.                     if((strcmp(TempEntry->Name, CURRENTDIR) != 0) && (strcmp(TempEntry->Name, PARENTDIR) != 0))
  318.                             {
  319. #endif
  320.                             if(ErrorHeader == TRUE)
  321.                                 {
  322.                                 printf("I can't rename following files:\n");
  323.                                 printf("-------------------------------\n");
  324.                                 ErrorHeader = FALSE;
  325.                                 }
  326.                             printf("'%s'\n", TempEntry->Name);
  327.                             SkipCount++;
  328.                             }
  329. #ifdef UNIX
  330.                         }
  331. #endif
  332.                     else
  333.                         {
  334.                         fprintf(FileHandle, "%s%c%s\n", TempEntry->INode, 0x09, TempEntry->Name);
  335.                         Count++;
  336.                         }
  337.  
  338.                     Temp2 = TempEntry->Next;
  339.                     free(TempEntry);
  340.                     TempEntry = Temp2;
  341.                     }
  342.  
  343.                 fclose(FileHandle);
  344.                 if(Count)
  345.                     printf("%d files processed\n", Count);
  346.                 if(SkipCount)
  347.                     printf("%d files skipped\n", SkipCount);
  348.                 if(Count)
  349.                     printf("Done.\n");
  350.                 }
  351.             else
  352.                 {
  353.                 TempEntry = FirstEntry;
  354.                 while(TempEntry);
  355.                     {
  356.                     Temp2 = TempEntry->Next;                    
  357.                     free(TempEntry);
  358.                     TempEntry = Temp2;
  359.                     }
  360.                 printf("** Can't open index file to write 'WFMHFile.idx'.\n");
  361.                 exit(EXIT_FAILURE);
  362.                 }
  363.             }
  364.         else
  365.             {
  366.             printf("** Can't open directory '%s'.\n", argv[2]);
  367.             exit(EXIT_FAILURE);
  368.             }
  369.         }
  370.  
  371.  
  372. /*                             ########### DECODING PART BEGINS ##############   */
  373.  
  374.     else
  375.  
  376.         if(argv[1][0] == 'D' || argv[1][0] == 'd' || (argv[1][0] == '-' && argv[1][1] == 'D') || (argv[1][0] == '-' && argv[1][1] == 'd'))
  377.             {
  378.             if((TempEntry = malloc(sizeof(struct Entry))))
  379.                 {
  380.                 if((FileHandle = fopen("WFMHFile.idx", "r")) == NULL)
  381.                     if((FileHandle = fopen("wfmhfile.idx", "r")) == NULL)
  382.                         FileHandle = fopen("WFMHFILE.IDX", "r");
  383.  
  384.                 if(FileHandle)
  385.                     {
  386.                     if(argv[1][0] == '-')
  387.                     printf("Decoding filenames in expanded mode...\n");
  388.                         else
  389.                     printf("Decoding filenames...\n");
  390.  
  391.                     Count = 0;
  392.                     while(EOF != fscanf(FileHandle, "%s", TempEntry->INode))
  393.                         {
  394.  
  395.                         do
  396.                             TempEntry->Name[0] = getc(FileHandle);
  397.                         while((TempEntry->Name[0] != 0xa) && (TempEntry->Name[0] != 9));
  398.  
  399.                         for(Index=0; Index<MAXNAMLEN; Index++)
  400.                             {
  401.                             TempEntry->Name[Index] = fgetc(FileHandle);
  402.                             if(((TempEntry->Name[Index]) == 0xa) || (TempEntry->Name[Index] == EOF))
  403.                                     break;
  404.                             }
  405.                         TempEntry->Name[Index] = 0x00;
  406.  
  407.  
  408.                     if(rename(TempEntry->INode, TempEntry->Name))
  409.                         {
  410.                         DecodeResult = FALSE;
  411.  
  412.                         if(argv[1][0] == '-')
  413.                             {
  414.                             Index = 0;
  415.                             while(TempEntry->INode[Index] != '\0')
  416.                                 {
  417.                                 TempEntry->INode[Index] = tolower((int)TempEntry->INode[Index]);
  418.                                 Index++;
  419.                                 }
  420.                             if(rename(TempEntry->INode, TempEntry->Name))
  421.                                 {
  422.                                 Index = 0;
  423.                                 while(TempEntry->INode[Index] != '\0')
  424.                                     {
  425.                                     TempEntry->INode[Index] = toupper((int)TempEntry->INode[Index]);
  426.                                     Index++;
  427.                                     }
  428.                                 if(rename(TempEntry->INode, TempEntry->Name) == 0)
  429.                                     {
  430.                                     Count++;
  431.                                     DecodeResult = TRUE;
  432.                                     }
  433.                                 }
  434.                             }
  435.  
  436.                         if(DecodeResult == FALSE)
  437.                             {
  438.                             if(ErrorHeader == TRUE)
  439.                                 {
  440.                                 printf("I can't restore following files:\n");
  441.                                 printf("--------------------------------\n");
  442.                                 ErrorHeader = FALSE;
  443.                                 }
  444.                             printf("'%s'\n", TempEntry->Name);
  445.                             SkipCount++;
  446.                             }
  447.                         }
  448.                     else
  449.                         Count++;
  450.                         }
  451.  
  452.                     fclose(FileHandle);
  453.                     free(TempEntry);
  454.                     if(Count)
  455.                         printf("%d files restored\n", Count);
  456.                     if(SkipCount)
  457.                         printf("%d files skipped\n", SkipCount);
  458.                     if(Count)
  459.                         printf("Done.\n");
  460.  
  461.                     if(SkipCount == 0)
  462.                         {
  463.                         if(remove("WFMHFile.idx") != 0)
  464.                             if(remove("wfmhfile.idx") != 0)
  465.                                 if(remove("WFMHFILE.IDX") != 0)
  466.                                     {
  467.                                     printf("** Can't remove index file.\n");
  468. #ifdef AMIGA
  469.                                     exit(EXIT_WARN);
  470. #endif
  471.                                     }
  472.                         }
  473.                     }
  474.                 else
  475.                     {
  476.                     free(TempEntry);
  477.                     printf("** Can't find index file!\n");
  478.                     exit(EXIT_FAILURE);
  479.                     }
  480.                 }
  481.             else
  482.                 {
  483.                 printf("** Can't allocate work buffer!\n");
  484.                 exit(EXIT_FAILURE);
  485.                 }
  486.  
  487.             }
  488.         else
  489.             {
  490.             printf("** Unknown operation.\n");
  491.             exit(EXIT_FAILURE);
  492.             }
  493.  
  494.     chdir(CurrentDir);
  495.     exit(EXIT_SUCCESS);
  496. }
  497.